package com.myhost;

import java.io.File;
import java.util.Arrays;

import com.myhost.FileListAdapter.Viewholder;
import com.myhost.model.FileInfo;
import com.myhost.utils.FileSorter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class SaveFileListAdapter extends FileListAdapter {

	public SaveFileListAdapter(LayoutInflater inflater, Context context) {
		super(inflater, context);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (currentFileData.fileInfos.size() == 0)
			return null;
		Viewholder holder;
		/**/
		FileInfo fInfo = currentFileData.fileInfos.get(position);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.filelistview, null, false);
			holder = new Viewholder(convertView);
			holder.icon = (ImageView) convertView.findViewById(R.id.fileicon);
			holder.name = (TextView) convertView.findViewById(R.id.filename);
			holder.date = (TextView)convertView.findViewById(R.id.filedate);
			holder.size = (TextView)convertView.findViewById(R.id.filesize);
		} else {
			holder = (Viewholder) convertView.getTag();
		}
		holder.icon.setImageDrawable(getIconDrawbel(fInfo.icon));
		holder.name.setText(fInfo.fileName);
		holder.size.setText(fInfo.size);
		holder.date.setText(fInfo.date);
		return convertView;
	}

	@Override
	protected void findFile(String filePath) {

		File f = new File(filePath);
		// Log.i("file", f.getName() + "---f first");
		if (f.exists() && f.canWrite()) {
			// Log.i("file", f.getName() + "---f first");
			File[] files = f.listFiles();
			alFileInfo.clear();
			Arrays.sort(files, new FileSorter(FileSorter.TYPE_DEFAULT));

			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				FileInfo fileInfo = new FileInfo();
				// Log.e("tag", fileInfo.toString());
				if (file.isDirectory()) {
					fileInfo.fileName = file.getName();
					fileInfo.filePath = file.getPath();
					fileInfo.icon = DIRECTORY;
					fileInfo.setDate(file.lastModified());
					fileInfo.IsDir = true;
					currentFileData.fileInfos.add(fileInfo);
					// Log.e("tag", "array" +
					// currentFileData.fileInfos.get(i).toString());
					// Log.i("file", file.getName() + "");
				}
			}
		}
	
	}

}
